package com.atguigu.gmall.hystrix;

import com.atguigu.gmall.component.RequestContext;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;

import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @author Jerry.Su
 * @Date 2022/2/21 9:09
 * @description hystrix策略，拷贝threadlocal中的数据到hystrix线程池
 */
public class TraceStrategy extends HystrixConcurrencyStrategy {

    @Override
    public <T> Callable<T> wrapCallable(Callable<T> callable) {
        Map<String, String> contextMap = RequestContext.getCopyOfContextMap();
        return () -> {
            try {
                RequestContext.setCopyOfContextMap(contextMap);
                return callable.call();
            } finally {
                RequestContext.clear();
            }
        };
    }
}
